_G.EXPORT_ASSERT_TO_GLOBALS = true
package.path = package.path..';../../?.lua'
require('lib/luaunit')

local Client = require("CSmsg/client")

_G.test_csmsg = {} --class
    function test_csmsg:setUp()
        -- set up tests
        -- self.args = 1
        -- print( 'test_csmsg:setUp' )
        os.execute('ls')
        os.execute('lua5.1 CSmsg/serverScoket.lua 127.0.0.1 12345 &')
        -- os.execute('lua5.1 CSmsg/serverGame.lua &')

        self.client = Client:New()
        local host = "127.0.0.1"
        local port = 12345
        local timeout = 0
        self.client:connect(host, port, timeout)
    end

    function test_csmsg:tearDown()
        -- some tearDown() code if necessary
        -- print( 'test_csmsg:tearDown' )
        print('kill lua server')
        os.execute('pkill -9 lua')
    end

    function test_csmsg:testSendQuestRoleInfo()
        -- arrange
        local msg = "hahaha\n"
        -- act
        local roleInfo = nil
       self.client:request(msg, function (msgback)
            roleInfo = msgback
            print("roleInfo ...",roleInfo)
            print("<<<<<<<<<<<<<<<<<<<<<<")
        end)
        -- assert
        -- assertEquals( roleInfo.id, 1)
        -- assertEquals( roleInfo.name,"chaos"  )
        assertEquals("hahaha", roleInfo)
    end

    function test_csmsg:testQuestTwoDiffMsg()
        -- arrange
        local msg1 = "questGameID"
        local msg2 = "questBuffID"
        local msgBack1 ,msgBack2
        -- act
        self.client:request(msg1..'\n', function(msgback)
            msgBack1 = msgback
        end)
        self.client:request(msg2..'\n', function(msgback)
            msgBack2 = msgback
        end)
        -- assert
        assertEquals( msg1, msgBack1 )
        assertEquals( msg2, msgBack2 )
    end

    function test_csmsg:testCoroutine()
        -- arrange
        local msg1 = "questGameID"
        local msg2 = "questBuffID"
        local msgBack1 ,msgBack2

        local runAsyncFunc = function ( func, ... )
            local current = coroutine.running
            func(function (  )
                coroutine.resume(current)
            end, ...)
            coroutine.yield()
        end

        local quest1 = function (callBack)
            -- body
            self.client:request(msg1..'\n', function(msgback)
                msgBack1 = msgback
            end)
        end
        local quest2 = function (callBack)
            -- body
            self.client:request(msg2..'\n', function(msgback)
                msgBack2 = msgback
            end)
        end
        local co = coroutine.create( function ()
            runAsyncFunc(quest1)
            runAsyncFunc(quest2)
        end)
        -- act
        print("co",coroutine.resume(co))
        print("co",coroutine.resume(co))
        -- assert
        assertEquals( msg1, msgBack1 )
        assertEquals( msg2, msgBack2 )
    end


    -- function test_csmsg:testFunctionName()
    --     local host = '127.0.0.1'
    --     local port = 12345
    --     local socket = require("socket")
    --     local sock = assert(socket.connect(host, port))
    --     sock:settimeout(0)
    --     local input, recvt, sendt, status
    --     input = "hello"
    --     if #input > 0 then
    --         print(">>>>>>>")
    --         assert(sock:send(input .. "\n"))
    --     end
    --     recvt, sendt, status = socket.select({sock}, nil, 1)
    --     while #recvt > 0 do
    --         local response, receive_status = sock:receive()
    --         if receive_status ~= "closed" then
    --             if response then
    --                 print(response)
    --                 recvt, sendt, status = socket.select({sock}, nil, 1)
    --             end
    --         else
    --             break
    --         end
    --     end
    --     -- arrange
    --     -- act
    --     -- assert
    --     assertEquals( 1, 1 )
    -- end


local lu = LuaUnit.new()
lu:setOutputType('tap')
os.exit(lu:runSuite())
